package test.notifications;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NotificationsActivity extends Activity {
    /** Called when the activity is first created. */
	private static final int NOTIFY_ME_ID = 1337;
	private Timer timer = new Timer();
	private int count = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b1 = (Button) findViewById(R.id.button1);
        Button b2 = (Button) findViewById(R.id.button2);
        b1.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		TimerTask task = new TimerTask() {
					@Override
					public void run() {
						notifyMe();
					}
        		};
        		timer.schedule(task, 5000);
        	}
        });
        b2.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		NotificationManager vir = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        		vir.cancel(NOTIFY_ME_ID);
        	}
        });
    }
    public void notifyMe() {
    	final NotificationManager domino = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    	Notification note = new Notification(R.drawable.icon, "This is a notification", System.currentTimeMillis());
    	PendingIntent tent = PendingIntent.getActivity(this, 0, new Intent(this, NdefMessage.class), 0);
    	note.setLatestEventInfo(this, "Notification Title", "This is a notification", tent);
    	note.number=++count;
    	note.defaults |= Notification.DEFAULT_SOUND;
    	domino.notify(NOTIFY_ME_ID, note);
    }
}